package com.h3c.web.common.helper;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.List;
import java.util.concurrent.TimeoutException;

import com.google.common.collect.Lists;

import net.rubyeye.xmemcached.KeyIterator;
import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.MemcachedClientBuilder;
import net.rubyeye.xmemcached.XMemcachedClientBuilder;
import net.rubyeye.xmemcached.exception.MemcachedException;
import net.rubyeye.xmemcached.utils.AddrUtil;

/**
 * @ClassName: XMemcachedClientHelper
 * @Description: for memcached
 * @author: d14287
 * @date: 2017年6月2日 下午4:59:08
 */
public class XMemcachedClientHelper {
	private static MemcachedClientBuilder builder = new XMemcachedClientBuilder(
			AddrUtil.getAddresses("10.90.3.116:11211"));
	private static MemcachedClient mClient;
	static {
		try {
			mClient = builder.build();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static List<String> getKeys() throws MemcachedException, InterruptedException, TimeoutException {
		List<String> keys = Lists.newArrayList();
		KeyIterator it = mClient.getKeyIterator(new InetSocketAddress("10.90.3.116", 11211));
		while (it.hasNext()) {
			String key = it.next();
			keys.add(key);
		}
		return keys;
	}
	public static Object getValue(String key) throws TimeoutException, InterruptedException, MemcachedException {
		return mClient.get(key);
	}
}
